package com.lmscn.lms.api.controller;

import com.jfinal.core.Controller;
import com.lmscn.lms.api.interceptor.Condition;
import com.lmscn.lms.api.interceptor.KeyCondition;
import com.lmscn.lms.api.model.*;
import com.lmscn.lms.api.util.CommonUtil;
import com.lmscn.lms.api.util.DateUtil;
import com.lmscn.lms.api.util.JsonKit;
import com.lmscn.lms.commons.GlobalConst;

import java.util.*;

/**
 * Created by Czl on 2015/7/24.
 */
public class CourseTopicController extends Controller {

	public Condition cond;
	public KeyCondition kcd;
	
	/**
	 * 课程专题：年度必修课选课目录
	 * @return
	 */
	public void courseListByTopicId() {
		// 获取当前登录的学员本年度的学习计划
		Long topicId = getParaToLong("topicId");
		String studyGroup = getPara("studyGroup");
		Long studentId = getParaToLong("studentId");
		int pageNumber = getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize");
		int start = (pageNumber - 1) * pageSize;
		int end = (pageNumber - 1) * pageSize + pageSize;
		
		
		int study_plan_year = Integer.parseInt(DateUtil.getYear());
		StudyPlan studyPlan = StudyPlan.dao.getStudyPlan(study_plan_year, studyGroup);
		Map<String, Object> dataMaps = new HashMap<String, Object>();
		List<Map<String, Object>> topicCourseList = new ArrayList<Map<String, Object>>();
		CourseTopic courseTopic = CourseTopic.dao.getCourseTopicById(topicId);
		
		String courseIds = courseTopic.getStr("course_ids");
		Long [] idAry = this.splitCourseIds(courseIds);
		int total = idAry.length;
		if(total >= start) {
			Map<String, Object> dataMap = null;
			CourseBase courseBase = null;
			
			if (courseIds != null && idAry.length > 0) {
				end = end<=total ? end : total; 
				for (int j=start; j<end; j++) {
					dataMap = new HashMap<String, Object>();
					courseBase = CourseBase.dao.findById(idAry[j]);
//					courseBase.set("url", CommonUtil.getMediaUrl(courseBase));
					courseBase.set("small_img", courseBase.getStr("small_img"));
					courseBase.set("large_img", courseBase.getStr("small_img"));
					dataMap.put("courseBase", courseBase);
					dataMap.put("courseLesson", courseBase.getCourseLesson());
					dataMap.put("courseLearningSetting", courseBase.getCourseLearningSetting());
					
					int isCheck = 0;
					Long cs_id = null;
					if (studentId>0) {
						CourseStudent cs = CourseStudent.dao.getCourseTopicCourseStudent(
								studyPlan.getLong("id"), (long)topicId, idAry[j],
								studentId, "studyplan");
						if (cs!=null && cs.getLong("id")!=null) {
							isCheck = 1;
							cs_id = cs.getLong("id");
						}
					}
					dataMap.put("courseStudentId", cs_id);
					dataMap.put("selected", isCheck);
					topicCourseList.add(dataMap);
					// 课程隐藏功能，2017年2月4日14:44:07 add by zhang.xx
//					if (courseBase != null) {
//						if (courseBase.getInt("level") != 0) {
//						}
//					}
				}
			}
		}
		
		dataMaps.put("courseList", topicCourseList);
		renderJson(JsonKit.mapToJson(dataMaps, 39));
	}
//	sc.setCategoryId(cb.getCategoryId());
//	sc.setCourseId(cb.getId());
//	sc.setCourseName(cb.getName());
//	sc.setCourseNo(cb.getCourseNo());
//	sc.setCoursePeriod(courseLearningSettingManager.getPeriodByCourseId(cb.getId()));
//	sc.setStudyNum(courseCountManager.getStudyNumByCourseId(cb.getId()));
//	sc.setTeacherName(cb.getTeacherName());
//	sc.setIsRecommend(cb.getIsRecommend()); 
//	CourseStudent courseStudent = null;
//	if(!empty(student)){
//		sc.setStudentId(student.getId());
//		
//		// 获取当前登录的学员本年度的学习计划
//		StudyPlan currentStudyPlan = super.getLoginStudentYearStudyPlan();						
//		
//		courseStudent = courseStudentManager.getStudyPlanCourseStudent(currentStudyPlan.getId(), cb.getId(), student.getId());
//		if(courseStudent != null){
//			sc.setCourseStudentId(courseStudent.getId());
//		}
//	}
	
	/**
	 * @Author:工具方法：将用逗号隔开的字符串转换成List
	 * @Description:分割课程id字符串
	 * @param courseIds	课程id字符串
	 * @return
	 */
	private Long [] splitCourseIds(String courseIds){
		if(courseIds == null || courseIds.trim().length() == 0){
			return null;
		}
		String [] idsStr =  courseIds.trim().split(",");
		if(idsStr != null && idsStr.length > 0){
			Long [] ids = new Long[idsStr.length];
			for(int i=0;i<idsStr.length;i++){
				try {
					ids[i] = Long.parseLong(idsStr[i]);
				} catch (Exception e) {}
			}
			return ids;
		}else{
			return null;
		}
	}
	
}
